<?php
namespace Admin\Controller;
use Common\Controller\CoreController;
class RoleController extends CoreController {

    //角色列表
    public function index(){
        //实例化角色模型
        $page = I('get.p',1,'intval');
        $roleModel = M('Role');
        $list = $roleModel->page($page.',20')->select();
        $this->assign('list',$list);

        $count = $roleModel->count();
        $Page = new \Think\Page($count,20);
        $show = $Page->show();
        $this->assign('page',$show);

        $this->assign('edit',U('editRole'));
        $this->assign('del',U('delRole'));
        $this->assign('addText','添加角色');
        $this->assign('addNew',U('addRole'));
        $this->display();
    }

    public function addRole(){
        if(IS_POST){
            $data = array(
                'name'=>I('post.name'),
                'status'=>I('post.status',0,'intval'),
                'remark'=>I('post.remark'),
                );
            $roleId = M('Role')->add($data);
            if($roleId){
                $data = array();
                $nodes = I('post.nodes');
                foreach ($nodes as $node) {
                    $temp = explode('_',$node);
                    $data[] = array('role_id'=>$roleId,'node_id'=>$temp[0],'level'=>$temp[1],'module'=>$temp[2]);
                    
                }
                M('Access')->addAll($data);
                header('Location:'.U('index'));
            }else{
               $this->error('操作失败'); 
            }
        }else{
            $nodeList = $this->nodeList(M('node')->order('id desc')->select(),1);
            $this->assign('action',U('addRole'));
            $this->assign('cancel',U('index'));
            $this->assign('nodeList',$nodeList);

            //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
            $this->setStaticFile('js/objs/role.js','foot','js');
            $this->display();
        }
    }

    public function editRole(){
        if(IS_POST){
            $id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $data = array(
                'name'=>I('post.name'),
                'status'=>I('post.status',0,'intval'),
                'remark'=>I('post.remark'),
                );
            $status = M('Role')->where("id=".$id)->save($data);

            $data = array();
            $nodes = I('post.nodes');
            foreach ($nodes as $node) {
                $temp = explode('_',$node);
                $data[] = array('role_id'=>$id,'node_id'=>$temp[0],'level'=>$temp[1],'module'=>$temp[2]);
                
            }
            M('Access')->where('role_id='.$id)->delete();
            M('Access')->addAll($data);
            header('Location:'.U('index'));

        }else{
            $nodeList = $this->nodeList(M('node')->order('id desc')->select(),1);
            $this->assign('action',U('editRole'));
            $this->assign('cancel',U('index'));
            $this->assign('nodeList',$nodeList);

            $id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }
            $this->assign('id',$id);

            $this->role = M('Role')->where('id='.$id)->find();

            $accessList = M('Access')->where('role_id='.$id)->select();
            foreach ($accessList as $key => $access) {
                $accessIds[] = $access['node_id'];
            }
            $this->assign('accessIds',$accessIds);
            //加载视图类型静态文件
            $this->setStaticFiles('AdvancedForm');
            $this->setStaticFile('js/objs/role.js','foot','js');
            $this->display();
        }
    }

    public function delRole(){

        $id = I('get.id',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        M('Role')->where("id=".$id)->delete();
        $del=M('Access')->where('role_id='.$id)->delete();
        // header('Location:'.U('index'));
        if($del){
            $this->success('删除成功','index');
        }else{
            $this->error('删除失败','index');
        }
    }

    //显示节点
    public function node(){
        $level = I('get.level',1,'intval');
        $pid = I('get.pid',1,'intval');
        $this->assign('level',$level);

        //获取节点列表
        $nodeModel = M('node');
        $list = $nodeModel->where("pid='%d'",array($pid))->order('id desc')->select();
        //获取节点上一层节点
        $node = $nodeModel->field('name,pid,level')->where("id='%d'",array($pid))->find();

        //设置返回链接
        if($node['pid']==1){
            $back = U('node');
        }elseif($node['pid']>1){
            $back = U('node',array('level'=>$node['level'],'pid'=>$node['pid']));
        }

        //设置添加新节点链接
        $this->addNew = U('addNode',array('level'=>$level,'pid'=>$pid));

        //设置添加按钮显示的文字
        if($level == 1){
            $nameText = '模块';
            $linkText = '控制器';
            $addText = '添加模块';
        }elseif($level == 2){
            $nameText = '控制器';
            $linkText = '方法';
            $addText = '添加控制器';
        }elseif($level == 3){
            $nameText = '方法';
            $addText = '添加方法';
        }
  
        $level++;
        foreach ($list as $key => $value) {
            if($level<4){
            $list[$key]['href'] = U('node',array('level'=>$level,'pid'=>$value['id']));
            }
            $list[$key]['edit'] = U('editNode',array('level'=>$value['level'],'id'=>$value['id']));
            $list[$key]['del'] = U('delNode',array('level'=>$value['level'],'pid'=>$pid,'id'=>$value['id']));
        }
        $this->assign('title',$node['name'].'的'.$nameText);
        $this->assign('back',$back);
        $this->assign('nameText',$nameText);
        $this->assign('addText',$addText);
        $this->assign('linkText',$linkText);
        $this->assign('list',$list);

        //加载视图类型静态文件
        $this->setStaticFiles('ResponsiveTable');
        $this->display();

    }

    //
    public function addNode(){
        if(IS_POST){
            $data = array(
                'name' => I('post.name'),//名称（与模块或者方法同名）
                'title' => I('post.title'),//中文名
                'status' => I('post.status',0,'intval'),//状态
                'type'=>I('post.type',0,'intval'),
                'sort' => I('post.sort',0,'intval'),//排序
                'remark' => I('post.remark'),//模块备注
                'pid' => I('post.pid',1,'intval'),//上级节点ID
                'level' => I('post.level',1,'intval'),//级别（1是模块，2是控制器，3是方法）
            );
            $nodeModel = M('node');
            if($nodeModel->add($data)){
                if($data['pid']==1){
                    $back = U('node');
                }elseif($data['pid']>1){
                    $back = U('node',array('level'=>$data['level'],'pid'=>$data['pid']));
                }
                header('Location:'.$back);

            }else{
                $this->error('操作失败');
            }
        }else{
            $level = I('get.level',1,'intval');
            $pid = I('get.pid',1,'intval');
            if($level==1){
                $formTitle='模块';
            }elseif($level==2){
                $formTitle='控制器';
            }elseif($level==3){
                $formTitle='方法';
            }
            $this->assign('formTitle',$formTitle);
            $this->assign('level',$level);
            $this->assign('pid',$pid);
            $this->cancel = U('node',array('level'=>$level,'pid'=>$pid));
            $this->action = U('addNode');
            $this->display();
        }
    }

    public function editNode(){
        if(IS_POST){
            $id = I('post.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }

            $data = array(
                'name' => I('post.name'),//名称（与模块或者方法同名）
                'title' => I('post.title'),//中文名
                'status' => I('post.status',0,'intval'),//状态
                'type'=>I('post.type',0,'intval'),
                'sort' => I('post.sort',0,'intval'),//排序
                'remark' => I('post.remark'),//模块备注
            );

            $nodeModel = M('node');
            if($nodeModel->where("id=".$id)->save($data)){ 
                $node = $nodeModel->where("id='%d'",array($id))->find();
                if($node['pid']==1){
                    $back = U('node');
                }elseif($node['pid']>1){
                    $back = U('node',array('level'=>$node['level'],'pid'=>$node['pid']));
                }
                header('Location:'.$back);

            }else{
                $this->error('操作失败');
            }
        }else{
            $id = I('get.id',0,'intval');
            if($id<1){
                $this->error('错误的ID');
            }

            $nodeModel = M('node');
            $node = $nodeModel->where("id='%d'",array($id))->find();

            if($node['level']==1){
                $formTitle='模块';
            }elseif($node['level']==2){
                $formTitle='控制器';
            }elseif($node['level']==3){
                $formTitle='方法';
            }

            $this->assign('formTitle',$formTitle);
            $this->assign('node',$node);

            $this->cancel = U('node',array('level'=>$node['level'],'pid'=>$node['pid']));
            $this->action = U('editNode');
            $this->display();
        } 
    }

    public function delNode(){
        $id = I('get.id',0,'intval');
        $pid = I('get.pid',0,'intval');
        $level = I('get.level',0,'intval');
        if($id<1){
            $this->error('错误的ID');
        }
        $nodeModel = M('node');
        $nodeModel->delete($id);
        $url = U('node',array('level'=>$level,'pid'=>$pid));
        $this->success('删除成功',$url, 2);
    }
}